package com.github.ghsea.cc.service.db;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.storm.shade.org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.ghsea.cc.module.InterceptedLog;
import com.github.ghsea.cc.repository.InterceptedLogDao;

@Service
public class InterceptedAccessServiceImpl implements InterceptedAccessService {

	private Logger log = LoggerFactory.getLogger(InterceptedAccessServiceImpl.class);

	@Resource
	private InterceptedLogDao interceptedLogDao;

	@Transactional
	public void insertOrUpdateByIp(InterceptedLog interLog) {
		Long logId = interceptedLogDao.queryNotExpiredLog(interLog.getIp(), interLog.getRuleId());
		Date now = new Date();
		Date expireTime = DateUtils.addHours(now, 2);
		interLog.setExpireTime(expireTime);
		if (logId == null) {
			interLog.setCreateTime(now);
			interceptedLogDao.insert(interLog);
		} else {
			interLog.setId(logId);
			interLog.setUpdateTime(now);
			interceptedLogDao.updateByPk(interLog);
		}
		System.out.println("拦截：" + interLog);

	}
}
